﻿using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.ServiceModel.DomainServices.Client;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using YouFood.ViewModel;
using YouFood.Web;
using YouFood.Web.Model;

namespace YouFood
{
    public partial class CookerView : Page, INotifyPropertyChanged
    {

        #region property

        private ObservableCollection<OrderSubViewModel> _ordersToCook;
        public ObservableCollection<OrderSubViewModel> OrdersToCook
        {
            get { return _ordersToCook; }
            set 
            {
                _ordersToCook = value;
                NotifyPropertyChanged("OrdersToCook"); 
            }
        }

        private ObservableCollection<OrderSubViewModel> _ordersCooked;
        public ObservableCollection<OrderSubViewModel> OrdersCooked
        {
            get { return _ordersCooked; }
            set
            {
                _ordersCooked = value;
                NotifyPropertyChanged("OrdersCooked");
            }
        }

        private OrderSubViewModel _orderCookedSelected;
        public OrderSubViewModel OrderCookedSelected
        {
            get { return _orderCookedSelected; }
            set
            {
                _orderCookedSelected = value;
                NotifyPropertyChanged("OrderCookedSelected");
            }
        }

        private OrderSubViewModel _orderToCookSelected;
        public OrderSubViewModel OrderToCookSelected
        {
            get { return _orderToCookSelected; }
            set
            {
                _orderToCookSelected = value;
                NotifyPropertyChanged("OrderToCookSelected");
            }
        }

        private OrderCookerDomainContext _context = new OrderCookerDomainContext();

        #endregion

        #region Constructor
        
        public CookerView()
        {
            if (!WebContext.Current.User.IsInRole("Cuisinier"))
                return; // Elle sont ou les acolade? Petit homosexuelle!!

            InitializeComponent();
            this.MyImage.Source = new BitmapImage(new Uri("/YouFood;component/Assets/Images/Fleche.png", UriKind.Relative));
            this.MyImage2.Source = new BitmapImage(new Uri("/YouFood;component/Assets/Images/Fleche.png", UriKind.Relative));
            this.MyImage3.Source = new BitmapImage(new Uri("/YouFood;component/Assets/Images/Refresh.png", UriKind.Relative));

            OrdersToCook = new ObservableCollection<OrderSubViewModel>();
            OrdersCooked = new ObservableCollection<OrderSubViewModel>();
            DataContext = this;
        }

        #endregion

        #region Methodes

        // OrderStatus
        // 0 - Commandé
        // 1 - en preparation
        // 2 - Preparé
        // 3 - Annulé

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {

            RefreshList();
        }

        private void RefreshList()
        {
            _context = new OrderCookerDomainContext();
            var query = _context.GetOrdersCookerQuery();
            var loadData = _context.Load(query);
            loadData.Completed += new EventHandler(loadData_Completed);
        }

        void loadData_Completed(object sender, EventArgs e)
        {
            var senders = (LoadOperation<Order>)sender;

            OrdersCooked.Clear();
            OrdersToCook.Clear();

            //Fill list à preparation
            
            OrdersToCook = new ObservableCollection<OrderSubViewModel>(senders.Entities.Where(x => x.Status == 0)
            .Select(item => new OrderSubViewModel(item))
            .OrderBy(x => x.OrderLocal.Date));

            OrdersCooked = new ObservableCollection<OrderSubViewModel>(senders.Entities.Where(x => x.Status == 1)
            .Select(item => new OrderSubViewModel(item))
            .OrderBy(x => x.OrderLocal.Date));
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(string info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }

        #endregion

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if(OrderToCookSelected != null)
            {
                OrderToCookSelected.OrderLocal.Status = 1;
                OrdersCooked.Add(OrderToCookSelected);
                OrdersToCook.Remove(OrderToCookSelected);
                _context.SubmitChanges();
            }
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            if (OrderCookedSelected != null)
            {
                OrderCookedSelected.OrderLocal.Status = 2;
                OrdersCooked.Remove(OrderCookedSelected);
                _context.SubmitChanges();
            }
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            RefreshList();
        }
        
    }
}
